package com.yxkj.sms;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/**
 * 描述:
 * 创建: Lzs
 * 时间: 2017/9/27
 */

public class SMSAdapter extends BaseAdapter {

    private SparseArray<String> sendStateList = new SparseArray<>();//发送状态
    private SparseArray<String> receiveList = new SparseArray<>();//发送状态
    private Context context;
    private List<String> datalist;
    private String str = "发送成功", sss = "收件人已接收";

    public SMSAdapter(Context context, List<String> datasContent, SparseArray<String> sendStateList, SparseArray<String> receiveList) {
        this.context = context;
        this.datalist = datasContent;
        this.sendStateList = sendStateList;
        this.receiveList = receiveList;
    }

    public void setSendStateList(SparseArray<String> sendStateList) {
        this.sendStateList = sendStateList;
        notifyDataSetChanged();
    }

    public void setDatalist(List<String> datalist) {
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    public void setReceiveList(SparseArray<String> receiveList) {
        this.receiveList = receiveList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        redGridViewHolder holder;
        if (convertView == null) {
            holder = new redGridViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            holder.tvLeftNumber = (TextView) convertView.findViewById(R.id.tv_left_number);
            holder.tvsend = (TextView) convertView.findViewById(R.id.tv_send);
            holder.tvreceive = (TextView) convertView.findViewById(R.id.tv_receive);
            holder.btnresend = (Button) convertView.findViewById(R.id.btn_resend);
            holder.tvRightContent = (TextView) convertView.findViewById(R.id.tv_right_content);
            convertView.setTag(holder);
        } else {
            holder = (redGridViewHolder) convertView.getTag();
        }
        String[] split = datalist.get(position).split("\\*\\*\\*");
        String number = split[0];
        final String content = split[1];
        holder.tvLeftNumber.setText(number);
        holder.tvRightContent.setText(content);

        //发送状态
        String s = (sendStateList == null ? "" : sendStateList.get(position));
        if (TextUtils.isEmpty(s)) {
            s="";
        }
        String r =( receiveList == null ? "" : receiveList.get(position));
        if (TextUtils.isEmpty(r)) {
            r="";
        }
        holder.tvsend.setText(s);
        holder.tvsend.setTextColor(s.equals(str) ? context.getResources().getColor(R.color.color) : context.getResources().getColor(R.color.red));

        holder.tvreceive.setVisibility(r.isEmpty() ? View.INVISIBLE : View.VISIBLE);
//        holder.btnresend.setVisibility(r.equals(sss) ? View.INVISIBLE : View.VISIBLE);
//        holder.btnresend.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ((MainActivity) context).SenMsg(position);
//                Log.e("activity_main","click"+position);
//            }
//        });
        return convertView;
    }

    static class redGridViewHolder {
        TextView tvLeftNumber, tvRightContent, tvsend, tvreceive;
        Button btnresend;
    }
}
